<?php
/**
 * 跃飞科技版权所有 @2017
 * User: 钟贵廷
 * Date: 2017/5/23
 * Time: 17:38
 */

namespace app\admin\controller;
use app\admin\model\Baidu;
use think\Db;
use think\Loader;
use think\Request;

class Article extends Admin
{
    public function lst($keyword = '')
    {
        $article_model =  model('article');
        $data = $article_model->search(trim($keyword));
        $article_data = $data["data"];
        $page = $data['page'];//分页
        //获取分类名
        $article_cat_model = model("ArticleCat");
        $cat_name =  $article_cat_model->getCatName();
        $this->assign("cat_name",$cat_name);
        $this->assign("article_data",$article_data);
        $this->assign("page",$page);
        $this->assign("keyword",input('keyword'));
        return $this->fetch();
    }


    /**
     * 添加文章
     */
    public function add()
    {
        if(Request::instance()->isPost())
        {
            $validate = Loader::validate('Article');
            $article = input('post.');
            if($validate->check($article))
            {
                $article['add_time'] = strtotime($article['add_time'])?:time();
                $articleModel = new \app\admin\model\Article();
                $data = $articleModel->upload($article);
                $article = array_merge($article,$data);
                $res = Db::name('article')->insert($article);
                if($res)
                {
                    //主动推送链接到百度收录
                    $article_id = Db::name('article')->getLastInsID();
                    $urls = url('home/news/detail',['id'=>$article_id],true,true);
                    $baiduModel = new Baidu();
                    $baiduModel->sendUrl($urls);
                    $this->success('添加成功', url('lst'));
                }
                else
                {
                    $this->success('添加失败');
                }
            }
            else
            {
                $this->error($validate->getError());
            }
        }
        //取出分类
        $article_cat = model('ArticleCat');
        $data =  $article_cat->field('cat_id,cat_name,parent_id')->select();
        $article_cat_data =  $article_cat->tree($data);
        $this->assign('article_cat_data', $article_cat_data);
        return $this->fetch();
    }


    /**
     * 修改文章
     * @param $article_id
     */
    public function save($article_id)
    {
        if(Request::instance()->isPost())
        {
            $validate = Loader::validate('Article');
            $article = input('post.');
            if($validate->check($article))
            {
                $article['add_time'] = strtotime($article['add_time'])?:time();
                $articleModel = new \app\admin\model\Article();
                $data = $articleModel->upload($article);
                $article = array_merge($article,$data);
                unset($article['old_img']);
                $res = Db::name('article')->update($article);
                if($res)
                {
                    $this->success('修改成功', url('lst'));
                }
                else
                {
                    $this->success('修改失败');
                }
            }
            else
            {
                $this->error($validate->getError());
            }
        }

        $article_model = model("Article");
        $article_data = $article_model->find($article_id);
        $this->assign("article_data", $article_data);
        //取出分类
        $article_cat = model('ArticleCat');
        $data =  $article_cat->field('cat_id,cat_name,parent_id')->select();
        $article_cat_data =  $article_cat->tree($data);
        $this->assign('article_cat_data', $article_cat_data);
        return $this->fetch();
    }


    /**
     * 删除文章
     * @param $article_id
     */
    public function delete($article_id)
    {
        $article_model = db('article');
        $article_data = $article_model->where(array('article_id'=>$article_id))->find();
        if($article_data)
        {
            //删除原图与缩略图
            $data[] = ROOT_PATH.'public'.$article_data['article_img'];
            $data[] = ROOT_PATH.'public'.$article_data['thumb2_img'];
            $data[] = ROOT_PATH.'public'.$article_data['thumb1_img'];
            foreach ($data as $k=>$v)
            {
                if(is_file($v))
                {
                    unlink($v);
                }
            }
            $article_model->delete($article_id);
            $this->success("删除成功！");
        }
        else
        {
            $this->error("手贱，滚蛋吧！");
        }
    }
}